//读取房台列表信息
function set_houses(houses) {
	var append_html = "";
	for(var g in houses) {
		var gd = houses[g];
		var cla = "";
		var status = "关";
		if(gd.status == 2) {
			cla = "class='kait'";
			status = "开";
		} else if(gd.status == 3) {
			cla = "class='kaid'";
			status = "下";
		} else if(gd.status == 4) {
			cla = "class='jiez'";
			status = "结";
		}
		append_html += '<li data-status=' + gd['status'] + ' data-id="' + gd['id'] + '"' + cla + '>' +
			'<div class="fantnambox clearfix">' +
			'<span class="fantnam">' + gd['name'] + '</span>' +
			'<span class="fanttype">' + status + '</span>' +
			'</div>' +
			'<div class="fantpri">' + gd['name'] + '</div>' +
			'<div class="fantnumbox clearfix">' +
			'<span class="fanttime">'+gd['nums']+'</span>人' +
			'<span class="fantnum">限' + gd['scope_limit'] + '人</span>' +
			'</div></li>';
	}
	return append_html;
}

function setEvent2() {
	$("#house_type li").click(function() {
		$('#house_type .active').removeClass("active");
		$(this).addClass("active");
		$type_id = $(this).attr('data-id');
		var lay = layer.load(1, {
			shade: [0.1, '#fff']
		});
		$.ajax({
			url: APP.api_url + "Sy/get_housesbt",
			data: {
				agent_id: getValue("agent_id"),
				type_id: $type_id,
				app_id: APP.app_id,
				time: Date.parse(new Date())
			},
			success: function(data) {
				layer.close(lay);
				var res = data.houses;
				if(res) {
					var append_html = set_houses(res);
					$(".fant_list").html(append_html);
					var add_html = '<div class="no_style"><p><img src="img/none.png"></p>' +
						'<p class="nr">请选择房台</p>' +
						'</div>';
					$("#goods_list1").html(add_html);
					house_click();
				}
			}
		})
	})
}

function add_num1(obj, id) {
	var num = parseFloat(cart[id]['num']);
	var zs = parseFloat(cart[id]['zs']);
	var dk = parseFloat(cart[id]['dk']);
	cart[id]['num'] = num + 1;
	cart[id]['total_zs'] = cart[id]['total_zs'] + zs;
	cart[id]['total_dk'] = cart[id]['total_dk'] + dk;
	$("#totals").text((parseFloat($("#totals").text()) + parseFloat(cart[id]['price'])).toFixed(2));
	$(obj).next("input").val(num + 1);
	check_stock($(obj).next("input"), id);
}
//房台点击事件
function house_click() {
	$(".fant_list li").css("cursor", "pointer");
	$(".fant_list li").click(function() {
		$('.fant_list .active').removeClass("active");
		$(this).addClass("active");
		var house_id = $(this).attr("data-id");
		var obj = $(this);
		$.ajax({
			url: APP.api_url + "Sy/get_houseinfo",
			data: {
				agent_id: getValue("agent_id"),
				house_id: house_id,
				app_id: APP.app_id,
				time: Date.parse(new Date())
			},
			success: function(data) {
				var res = data.house;
				var order = data.data;
				var add_html = "";
				$('#house_name').text(res['name']);
				if(data['code'] == 404) {
					add_html = '<div class="no_style"><p><img src="img/none.png"></p>' +
						'<p class="nr">该房台还未开台哦~</p>' +
						'</div>';
						$('#htotal').text(0);
				} else if(data['code'] == 406) {
					add_html = '<div class="no_style"><p><img src="img/none.png"></p>' +
						'<p class="nr">已开台还未下单~</p>' +
						'</div>';
						$('#htotal').text(0);
				} else {
					add_html = '<li><div class="pronam">名称</div><div class="projg">单价</div><div class="pronum">数量</div><div class="pronum">操作</div></li>';
					if(order) {
						obj.attr("data-o", order['id']);
						var items = order['item'];
						$('#htotal').text(order['total_price']);
						for(var g in items) {
							var gd = items[g];
							add_html += '<li>' +
								'<div class="pronam">' + gd['name'] + '</div>' +
								'<div class="projg">' + gd['price'] + '</div>' +
								'<div class="pronum">' + gd['num'] +
								'</div>' +
								'<div class="pronum"><img onclick="del_gd(this)" data-num="' + gd['num'] + '" data-dk"' + gd['dk_price'] +
								'" data-zs="' + gd['zs_price'] + '" data-price="' + gd['price'] + '" data-id="' + gd['id'] + '"  data-o="' + gd['order_id'] +
								'" src="img/del.png"/>' +
								'</div>' +
								'</li>';
						}
					}
				}
				if(res['status']!=1){
					$('#add_time').text(res['add_time']);
				}else{
					$('#htotal').text(0);
					$('#add_time').text('');
				}
				$("#goods_list1").html(add_html);
			}
		})
	})
}

function del_gd(obj) {
	var status = $(".fant_list .active").attr('data-status');
	if(status == 4) {
		layer.msg("已结账请关台");
		return;
	}
	layer.confirm("确定删除该条商品?", {
		btn: ['确定', '取消'] //按钮
	}, function() {
		var order_id = $(obj).attr('data-o');
		var item_id = $(obj).attr('data-id');
		var price = $(obj).attr('data-price');
		var dk_price = $(obj).attr('data-dk');
		var zs_price = $(obj).attr('data-zs');
		var num = $(obj).attr('data-num');
		$.ajax({
			type: 'get',
			url: APP.api_url + "Sy/del_gd",
			dataType: 'json',
			data: {
				order_id: order_id,
				item_id: item_id,
				price: price,
				dk_price: dk_price,
				zs_price: zs_price,
				num: num,
				app_id: APP.app_id,
				time: Date.parse(new Date())
			},
			success: function(data) {
				layer.msg(data.msg);
				if(data.code == 200) {
					var htotal = $("#htotal");
					var ht = parseFloat(htotal.text());
					var del_price = parseFloat(price) * parseFloat(num);
					htotal.text((ht - del_price).toFixed(2));
					$(obj).parent().parent().remove();
				}
			}
		});
	});

}

function get_houses() {
	var index = layer.load(1, {
		shade: [0.1, '#fff']
	});
	$.ajax({
		url: APP.api_url + "Sy/get_houses",
		data: {
			agent_id: getValue("agent_id"),
			app_id: APP.app_id,
			time: Date.parse(new Date())
		},
		success: function(data) {
			layer.close(index);
			var type = data.type;
			var type_html = "";
			if(type) {
				for(var t in type) {
					type_html += "<li data-id=" + type[t]['id'] + "><a>" + type[t].type_name + "</a></li>";
				}
				$("#house_type").append(type_html);
				setEvent2();
			}
			var res = data.res;
			if(res) {
				list = res;
				var append_html = "";
				for(var r in res) {
					var houses = res[r];
					append_html += set_houses(houses);
				}
				$(".fant_list").html(append_html);
				house_click();
			}
		}
	})
}
//开台
$(".kaits").click(function() {
	var obj = $(".fant_list .active");
	var id = obj.attr("data-id");
	var status = obj.attr("data-status");

	if(id == undefined) {
		layer.msg("您未选择房台");
		return;
	} else {
		if(status != 1) {
			layer.msg("该房台已开!");
			return;
		}
		if(status == 4) {
			layer.msg("已结账请先关台");
			return;
		}
		$.ajax({
			type: "post",
			data: {
				house_id: id,
				status: 2
			},
			url: APP.api_url + "sy/update_house",
			success: function(data) {
				layer.msg(data.msg);
				if(data.code == 200) {
					$("#add_time").text(data.add_time);
					obj.attr("data-status", 2);
					obj.addClass("kait");
					obj.find(".fantnambox").find(".fanttype").text("开");
					$(".nr").text("已开台请尽快下单");
				}
			}
		});
	}
})
//关台
$(".guants").click(function() {
	var obj = $(".fant_list .active");
	var id = obj.attr("data-id");
	var status = obj.attr("data-status");
	if(id == undefined) {
		layer.msg("您未选择房台");
		return;
	} else {
		if(status == 1) {
			layer.msg("该房台已关!");
			return;
		}
		$.ajax({
			type: "post",
			data: {
				house_id: id,
				status: 1
			},
			url: APP.api_url + "sy/update_house",
			success: function(data) {
				layer.msg(data.msg);
				if(data.code == 200) {
					$("#add_time").text();
					obj.attr("data-status", 1);
					obj.removeClass("kait");
					obj.removeClass("kaid");
					obj.removeClass("jiez");
					$("#goods_list1").html("");
					$("#htotal").text(0);
					obj.find(".fantnambox").find(".fanttype").text("关");
					add_html = '<div class="no_style"><p><img src="img/none.png"></p>' +
						'<p class="nr">还未开台哦~</p>' +
						'</div>';
					$("#goods_list1").html(add_html);
				}
			}
		});
	}
})
//下单
$(".diands").click(function() {
	var obj = $(".fant_list .active");
	var id = obj.attr("data-id");
	var status = obj.attr("data-status");
	if(id == undefined) {
		layer.msg("您未选择房台");
		return;
	}
	if(status == 1) {
		layer.msg("请先开台在点单");
		return;
	}
	if(status == 4) {
		layer.msg("已结账请关台");
		return;
	}
	$("#house_show").hide();
	$("#goods_show").fadeIn();
	get_gooods();
})
$(".dayins").click(function() {
	var obj=$(".fant_list .active");
	var id = obj.attr("data-id");
	var status=obj.attr("data-status");
	var order_id=obj.attr("data-o");
	if(id == undefined) {
		layer.msg("未选择房台");
		return;
	}
	if(status == 1) {
		layer.msg("未开台");
		return;
	}
	if(status == 2) {
		layer.msg("未点单");
		return;
	}
	print_item(order_id);
})
//返回操作
function back() {
	$("#house_show").fadeIn();
	$("#goods_show").hide();
}

function sao_pay() {
	var obj = $(".fant_list .active");
	var house_id = obj.attr("data-id");
	var status = obj.attr('data-status');
	var order_id=obj.attr('data-o');
	if(house_id == undefined) {
		layer.msg("您未选择房台");
		return;
	}
	if(status==1) {
		layer.msg("请先开台");
		return;
	}
	if(status==2) {
		layer.msg("请先点单");
		return;
	}
	if(status==4) {
		layer.msg("已结账请关台");
		return;
	}
	var price = parseFloat($("#htotal").text());
	$.ajax({
		url: APP.api_url + "Sy/get_housesk",
		type: 'POST',
		data: {
			agent_id: getValue("agent_id"),
			emp_id: getValue("id"),
			price: price,
			order_id: order_id,
			chain_id: getValue("chain_id"),
			house_id: house_id,
			app_id: APP.app_id,
			time: Date.parse(new Date())
		},
		success: function(data) {
			var agent = data.agent;
			if(agent.is_score == 1) {
				var price_html = "";
				if(agent.score >= data.total_zs) {
					var zs = data.total_zs;
					var dk = data.total_dk;
					price_html = '<p>零售价:¥<em>' + price + '</em>送<em>' + zs + '</em></p>' +
						'<p>会员价:¥<em>' + price + '</em>抵<em>' + dk + '</em></p>';
				} else {
					price_html = "<p>收款:¥<em>" + price + "</em></p>";
				}
			} else {
				price_html = "<p>收款:¥<em>" + price + "</em></p>";
			}
				var html = '<div class="sy_tck">' +
					'<div class="sy_ewmbox">' +
					'<input  type="hidden" id="oid" value="' + data.order_id + '"> ' +
					'<div><input  type="text" name="mem_no" id="mem_no" class="sy_sptsrk" style="width: 75%;border-top-left-radius: 10px;border-bottom-left-radius: 10px;" placeholder="请输入或扫描会员码"></div>' +
					'</br>' +
					'<div><input  type="text" name="mem_card" id="mem_card" class="sy_sptsrk" style="width: 75%;border-top-left-radius: 10px;border-bottom-left-radius: 10px;" placeholder="请输入或扫描会员付款码"></div>' +
					'</br>' +
					'<div class="sy_tctip">' + price_html +
					'</div>' +
					'</div>' +
					'<div class="sy_tcbtnbox">' +
					'<a href="#" class="dayin" onclick="print_order(' + data.order_id + ')">打印</a>' +
					'<a href="#" onclick="wanchen(1)" class="wanc">完成</a>' +
					'</div>' +
					'</div>';
			layer.open({
				title: '收款码',
				shadeClose: true,
				shade: 0.8,
				btn: false,
				area: ['380px', '450px'],
				content: html
			});
			set_keyup(4);
		}
	})
}
function pay_ok(){
	var obj = $(".fant_list .active");
	obj.removeClass("kaid");
	obj.addClass("jiez");
	obj.find(".fantnambox").find(".fanttype").text("结");
}
//支付完成
function wc(){
	var obj = $(".fant_list .active");
	var order_id = obj.attr('data-o');
		$.ajax({
			type: "post",
			data: {
				order_id:order_id,
				app_id: APP.app_id,
				time: Date.parse(new Date())
			},
			url: APP.api_url + "sy/ck_order",
			success: function(data) {
				layer.msg(data.msg);
				if(data.code == 200) {
					obj.removeClass("kaid");
					obj.addClass("jiez");
					obj.find(".fantnambox").find(".fanttype").text("结");
				}
			}
		});
	
}
//记账
function jiz_tck() {
	var obj = $(".fant_list .active");
	var house_id = obj.attr("data-id");
	var status = obj.attr('data-status');
	var order_id = obj.attr('data-o');
	if(house_id == undefined) {
		layer.msg("您未选择房台");
		return;
	}
	if(status == 1) {
		layer.msg("请先开台");
		return;
	}
	if(status == 2) {
		layer.msg("请先点单");
		return;
	}
	if(status == 4) {
		layer.msg("已结账请关台");
		return;
	}
	var con = layer.confirm('确认操作?', function(index) {
		$.ajax({
			url: APP.api_url + "Sy/house_jz",
			data: {
				agent_id: getValue("agent_id"),
				emp_id: getValue("id"),
				price: $("#htotal").text(),
				order_id: order_id,
				chain_id: getValue("chain_id"),
				house_id: house_id,
				app_id: APP.app_id,
				time: Date.parse(new Date())
			},
			success: function(data) {
				layer.close(con);
				if(data.code == 200) {
					obj.removeClass("kaid");
					obj.addClass("jiez");
					obj.find(".fantnambox").find(".fanttype").text("结");
				}
				var html = '<div class="sy_tck">' +
					'<div class="sy_ewmbox">' +
					'<div class="sy_cgnr">' +
					'<div class="iconfont icon-chenggong"></div>' +
					'</br><p>' + data.msg + '</p>' +
					'</div>' +
					'<div class="sy_tctip">' +
					'<p>¥<em>' + $("#htotal").text() + '</em></p>' +
					'</div>' +
					'</div>' +
					'<div class="sy_tcbtnbox">' +
					'<a href="#" class="dayin" onclick="print_order(' + order_id + ')">打印</a>' +
					'<a href="#" onclick="wanchen(3)" class="wanc">完成</a>' +
					'</div>' +
					'</div>';

				layer.open({
					title: '记帐收银',
					shadeClose: true,
					shade: 0.8,
					btn: false,
					area: ['380px', '450px'],
					content: html
				});
			}
		})
	});
}
function qr(o){
	$("#tanchu").fadeIn();
	var obj = $(".fant_list .active");
	var nums=obj.find(".fanttime").text();
	$("#people_nums").val(nums);
}
function cans(){
	$("#tanchu").hide();
	$("#memo").val("");
}
function clearNoNum(obj) {
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}
//点单确认操作
function qr2(o) {
	$(o).attr({
		"disabled": "disabled"
	});
	var nums=$("#people_nums").val();
	var obj = $(".fant_list .active");
	var ta 	=$("#memo");
	var house_id = obj.attr("data-id");
	var status = obj.attr('data-status');
	if(house_id == undefined) {
		layer.msg("您未选择房台");
		return;
	}
	if($.isEmptyObject(cart)) {
		layer.msg("未添加任何商品");
		return;
	}
	$.ajax({
		type: "post",
		data: {
			data: cart,
			people_nums:nums,
			memo:ta.val(),
			status: status,
			agent_id: getValue("agent_id"),
			chain_id: getValue("chain_id"),
			house_id: house_id,
			emp_id: getValue("id"),
			price: parseFloat($("#totals").text()),
			app_id: APP.app_id,
			time: Date.parse(new Date())
		},
		url: APP.api_url + "sy/add_horder",
		success: function(data) {
			$(o).removeAttr("disabled"); //将按钮可用
			cans();
			if(data.code == 200) {
				obj.find(".fanttime").text(nums);
				$("#totals").text(0);
				$("#nums").text(0);
				obj.attr("data-o", data['order_id']);
				var item = data.items;
				var add_html = '<li><div class="pronam">名称</div><div class="projg">单价</div><div class="pronum">数量</div><div class="pronum">操作</div></li>';
				for(var i in item) {
					var gd = item[i];
					add_html += '<li>' +
						'<div class="pronam">' + gd['name'] + '</div>' +
						'<div class="projg">' + gd['price'] + '</div>' +
						'<div class="pronum">' + gd['num'] +
						'</div>' +
						'<div class="pronum"><img onclick="del_gd(this)" data-num="' + gd['num'] + '" data-dk="' + gd['dk_price'] +
						'" data-zs="' + gd['zs_price'] + '" data-price="' + gd['price'] + '" data-id="' + gd['id'] + '"  data-o="' + gd['order_id'] +
						'" src="img/del.png"/>' +
						'</div>' +
						'</li>';
				}
				$("#htotal").text(data.total_price);
				$('#goods_list1').html(add_html);
				cart = {};
				$("#goods_list").html("");
				$("#house_show").fadeIn();
				$("#goods_show").hide();
				obj.attr("data-status", 3);
				obj.addClass("kaid");
				obj.find(".fantnambox").find(".fanttype").text("下");
			}
			layer.msg(data.msg);
		}
	})
}
//商品点击事件
function goods_click() {
	$(".sy_prolist li").css("cursor", "pointer");
	$(".sy_prolist li").click(function() {
		var goods_id = $(this).data("id");
		var type_id=$(this).data("tp");
		var price = parseFloat($(this).data('price'));
		var name = $(this).data('name');
		var good = cart[goods_id];
		var zs_price = $(this).data("zs");
		var dk_price = $(this).data("dk");
		if(good) {
			good['num'] = good['num'] + 1;
			good['total_zs'] = good['total_zs'] + zs_price;
			good['total_dk'] = good['total_dk'] + dk_price;
			cart[goods_id] = good;
			var gd = $("#gd_" + goods_id);
			var gd_num = parseFloat(gd.val());
			gd.val(gd_num + 1);
		} else {
			$("#nums").text(parseFloat($("#nums").text()) + 1)
			good = {};
			good["type_id"] = type_id;
			good["id"] = goods_id;
			good['price'] = price;
			good['name'] = name;
			good['num'] = 1;
			good['total_zs'] = zs_price;
			good['total_dk'] = dk_price;
			good['zs'] = zs_price;
			good['dk'] = dk_price;
			cart[goods_id] = good;
			var add_html = '<li>' +
				'<div class="pronam">' + name + '</div>' +
				'<div class="projg">' + price + '</div>' +
				'<div class="pronum">' +
				'<span onclick="add_num(this,' + goods_id + ')">+</span>' +
				'<input  onchange="change(this,' + goods_id + ')"    id="gd_' + goods_id + '" type="text" value=1 />' +
				'<span onclick="ms_num(this,' + goods_id + ')">-</span>' +
				'</div>' +
				'</li>';
			$("#goods_list").append(add_html);
		}
		$("#totals").text((parseFloat($("#totals").text()) + parseFloat(price)).toFixed(2));
	})
}

//商品分类点击事件
function setEvent() {
	$(".sy_pronav li").click(function() {
		$('.sy_pronav .active').removeClass("active");
		$(this).addClass("active");
		if(!$.isEmptyObject(list)) {
			var type_id = $(this).attr('data-id');
			var append_html = "";
			if(type_id) {
				var goods = list[type_id];
				append_html = set_goods(goods);
			} else {
				if(list) {
					for(var r in list) {
						var goods = list[r];
						append_html += set_goods(goods);
					}
				}
			}
			$(".sy_prolist").html(append_html);
			goods_click();
		}
	})
}

function get_gooods() {
	var index = layer.load(1, {
		shade: [0.1, '#fff']
	});
	$.ajax({
		url: APP.api_url + "Sy/goods_sk",
		data: {
			agent_id: getValue("agent_id"),
			chain_id: getValue("chain_id"),
			app_id: APP.app_id,
			time: Date.parse(new Date())
		},
		success: function(data) {
			layer.close(index);
			var type = data.type;
			var type_html = "";
			if(type) {
				$("#goods_type").html("");
				for(var t in type) {
					type_html += "<li data-id=" + type[t]['id'] + "><a>" + type[t].type_name + "</a></li>";
				}
				$("#goods_type").append(type_html);
				setEvent();
			}
			var res = data.res;
			if(res) {
				list = res;
				var append_html = "";
				for(var r in res) {
					var goods = res[r];
					append_html += set_goods(goods);
				}
				$(".sy_prolist").html(append_html);
				goods_click();
			}
		}
	})
}

function clear_cart1() {
	$('#goods_list').html('');
	$('#nums').text(0);
	$("#totals").text(0.00);
	cart = {};
}